package com.lechuang.cunpinke.manager;

import android.text.TextUtils;

import com.lechuang.cunpinke.BuildConfig;
import com.lechuang.cunpinke.app.MyApplication;
import com.lechuang.cunpinke.bean.LogBean;
import com.lechuang.cunpinke.db.dao.LogDao;
import com.lechuang.cunpinke.utils.FileUtil;
import com.tencent.bugly.crashreport.CrashReport;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

/**
 * 作者：尹松涛
 * 邮箱：yinsongtaoshmily@outlook.com
 * 日期：2017/6/23
 * 时间：10-56
 */

public class LogManager {
    private static volatile LogManager instance;
    private LogDao logDao;
    private List<LogBean> logs;
    private long lastInsertTime = 0;
    private boolean debug = false;

    private LogManager() {
        logDao = new LogDao();
        logs = new ArrayList<>();
    }

    public static LogManager getInstance() {
        if (instance == null) {
            synchronized (LogManager.class) {
                if (instance == null) {
                    instance = new LogManager();
                }
            }
        }
        return instance;
    }

    public synchronized void put(LogBean log) {
        if (!BuildConfig.DEBUG) {
            if (!debug) {
                return;
            }
        }
        logs.add(log);
        if (System.currentTimeMillis() - lastInsertTime > 10000 && !logs.isEmpty()) {
            lastInsertTime = System.currentTimeMillis();
            logDao.insertLogs(logs);
            logs.clear();
        }
    }

    public synchronized void loadDebugData(String uid) {
        if (TextUtils.isEmpty(uid)) {
            return;
        }

        //在这里控制开启日志记录


    }

    /**
     * 用于开发的时候导出数据库文件方便查看日志
     */
    public void saveLogToSdcard() {
        if (logDao.getAllLogs().isEmpty()) {
            return;
        }
        if (!logs.isEmpty()) {
            logDao.insertLogs(logs);
            logs.clear();
        }
        Observable.create(new Observable.OnSubscribe<Boolean>() {

            @Override
            public void call(Subscriber<? super Boolean> subscriber) {
                try {
                    File dbFile = new File(MyApplication.getContext().getDatabasePath("cache") + ".db");
                    File file = new File(FolderManager.getCrashLogFolder(), System.currentTimeMillis() + ".db");
                    FileUtil.copyFile(dbFile, file);
                    subscriber.onNext(true);
                } catch (IOException e) {
                    e.printStackTrace();
                    subscriber.onNext(false);
                }
            }
        }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<Boolean>() {
                    @Override
                    public void call(Boolean aBoolean) {
                        if (aBoolean.booleanValue()) {
                            ToastManager.getInstance().showLongToast("日志导出成功");
                            logDao.clear();
                        } else {
                            ToastManager.getInstance().showLongToast("日志导出失败");
                        }
                    }
                });
    }

    public void uploadLogs() {
        try {
            List<LogBean> allLogs = logDao.getAllLogs();
            if (allLogs == null || allLogs.isEmpty()) {
                return;
            }
            if (!logs.isEmpty()) {
                logDao.insertLogs(logs);
                logs.clear();
            }

            final String name = "关联用户的文件名";
            File dbFile = new File(MyApplication.getContext().getDatabasePath("cache") + ".db");
            if (dbFile == null || !dbFile.exists()) {
                return;
            }
            //处理文件保存位置
        } catch (Exception e) {
            CrashReport.postCatchedException(e);
        }
    }


}
